package com.cskaoyan.service.impl;

import com.cskaoyan.bean.bo.AdListBO;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.pojo.MarketGoods;
import com.cskaoyan.bean.pojo.MarketTopic;
import com.cskaoyan.bean.pojo.MarketTopicExample;
import com.cskaoyan.bean.vo.TopicListVO;
import com.cskaoyan.bean.vo.TopicReadVO;
import com.cskaoyan.bean.vo.WxTopicDetailVO;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.cskaoyan.service.TopicService;
import com.cskaoyan.util.Constant;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 专项管理实现类
 *
 * @author chenxu
 * @since 2022/04/07 17:25
 */
@Service
public class TopicServiceImpl implements TopicService {
    //注入实现类
    @Autowired
    MarketTopicMapper topicMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;

    @Override
    public TopicListVO list(AdListBO adListBO) {
        //自动拼接
        PageHelper.startPage(adListBO.getPage(),adListBO.getLimit());
        MarketTopicExample example = new MarketTopicExample();
        //设置查询结果的排序条件
        example.setOrderByClause(adListBO.getSort()+" "+adListBO.getOrder());
        MarketTopicExample.Criteria criteria = example.createCriteria();
        //动态添加查询的条件
        criteria.andDeletedEqualTo(false);
        if (adListBO.getTitle() != null){
           criteria.andTitleLike("%"+adListBO.getTitle()+"%");
        }
        if (adListBO.getSubtitle() != null ){
            criteria.andSubtitleLike("%"+adListBO.getSubtitle()+"%");
        }

        List<MarketTopic> marketTopics = topicMapper.selectByExampleWithBLOBs(example);

        //获取总的页码数和信息数量
        PageInfo<MarketTopic> pageInfo = new PageInfo<>(marketTopics);
        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();
        //将获取的数据封装给返回的对象
        TopicListVO topicListVO = new TopicListVO();
        topicListVO.setPage(adListBO.getPage());
        topicListVO.setLimit(adListBO.getLimit());
        topicListVO.setList(marketTopics);
        topicListVO.setPages(pages);
        topicListVO.setTotal(total);
        return topicListVO;
    }
    /*
     *专题信息删除
     *
     * @param marketTopic
     * @return void
     * @author chenxu
     * @since 2022/04/07 20:30
     */
    @Override
    public void delete(MarketTopic marketTopic) {
        //这里删除完成，将deleteId的值设为true
        marketTopic.setDeleted(true);
        //然后更新一下该条数据
        topicMapper.updateByPrimaryKey(marketTopic);
    }
    /*
     *多条记录删除
     *
     * @param ids
     * @return void
     * @author chenxu
     * @since 2022/04/07 22:55
     */
    @Transactional
    @Override
    public void batchDelet(Integer[] ids) {
        for (Integer id : ids) {
            //先根据id查找对应的数据，然后修改里面的deleteId状态
            MarketTopic marketTopic = topicMapper.selectByPrimaryKey(id);
            marketTopic.setDeleted(true);
            //更新状态
            topicMapper.updateByPrimaryKey(marketTopic);
        }
    }
    /*
     *增加信息
     *
     * @param marketTopic
     * @return com.cskaoyan.bean.pojo.MarketTopic
     * @author chenxu
     * @since 2022/04/07 22:55
     */
    @Override
    public MarketTopic create( MarketTopic marketTopic) {
        //set 添加的时间和修改的时间
        marketTopic.setAddTime(new Date());
        marketTopic.setUpdateTime(new Date());
        marketTopic.setDeleted(false);
        //新增信息
        topicMapper.insert(marketTopic);

        return marketTopic;
    }
   //todo
    @Override
    public MarketTopic update(MarketTopic marketTopic) {
        marketTopic.setUpdateTime(new Date());
        topicMapper.updateByPrimaryKey(marketTopic);
        MarketTopic marketTopic1 = topicMapper.selectByPrimaryKey(marketTopic.getId());
        return marketTopic1;
    }


    /*
     *读取专项编辑的信息
     *
     * @param id
     * @return com.cskaoyan.bean.vo.TopicReadVO
     * @author chenxu
     * @since 2022/04/08 11:34
     */
    @Transactional
    @Override
    public TopicReadVO read(Integer id) {
        MarketTopic marketTopic = topicMapper.selectByPrimaryKey(id);
        //获得goods数组，里面的值就是goods的Id值，通过id值获取goods的
        Integer[] goods = marketTopic.getGoods();

        //创建一个新的list封装goods
        List<TopicReadVO.GoodsListDTO> goodsList = new ArrayList<>();
        //遍历查找goods信息
        for (Integer goodId : goods) {
            MarketGoods marketGoods = goodsMapper.selectByPrimaryKey(goodId);
            if (!marketGoods.getDeleted()) {
                TopicReadVO.GoodsListDTO goodsListDTO = new TopicReadVO.GoodsListDTO(marketGoods.getId(), marketGoods.getName(),
                        marketGoods.getBrief(), marketGoods.getPicUrl(),
                        marketGoods.getIsNew(), marketGoods.getIsHot(),
                        marketGoods.getCounterPrice(), marketGoods.getRetailPrice());
                //将对象放进数组中
                goodsList.add(goodsListDTO);
            }
        }

        //将marketTopic和goodsList封装进需要返回的对象中
        TopicReadVO topicReadVO = new TopicReadVO();
        topicReadVO.setTopic(marketTopic);
        topicReadVO.setGoodsList(goodsList);
        return topicReadVO;
    }


    // 微信小程序端：by成文琪--------------------------
    @Override
    public TopicListVO queryWxTopicList(BaseParam param) {
        Integer limit = param.getLimit();
        Integer page = param.getPage();
        // 开启分页
        PageHelper.startPage(page, limit);
        // 查询market_topic表中所有记录
        MarketTopicExample example = new MarketTopicExample();
        // 按照sort_order字段降序排序
        example.setOrderByClause("sort_order desc");
        MarketTopicExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<MarketTopic> topicList = topicMapper.selectByExampleWithBLOBs(example);
        // 把不需要的字段设为null
        for (MarketTopic topic : topicList) {
            topic.setSortOrder(null);
            topic.setGoods(null);
            topic.setAddTime(null);
            topic.setUpdateTime(null);
            topic.setDeleted(null);
            topic.setContent(null);
        }
        // 获取分页信息
        PageInfo<MarketTopic> pageInfo = new PageInfo<>(topicList);
        int pages = pageInfo.getPages();
        long total = pageInfo.getTotal();

        return new TopicListVO(total, pages, limit, page, topicList);
    }

    @Transactional
    @Override
    public WxTopicDetailVO queryWxTopicDetail(Integer id) {
        // 首先根据id获取topic
        MarketTopic topic = topicMapper.selectByPrimaryKey(id);
        // 根据topic对象里的goods，去market_goods表里查找商品信息
        Integer[] goodsIds = topic.getGoods();
        List<MarketGoods> goodsList = new ArrayList<>();
        for (Integer goodsId : goodsIds) {
            MarketGoods goods = goodsMapper.selectByPrimaryKey(goodsId);
            if (!goods.getDeleted()) {
                goodsList.add(goods);
            }
        }
        return new WxTopicDetailVO(topic, goodsList);
    }

    @Override
    public TopicListVO queryWxTopicRelated(Integer id) {
        int limit = Constant.topicRelatedLimit;
        int page = Constant.topicRelatedPage;
        // 开启分页
        PageHelper.startPage(page, limit);
        // 获取全部专题，按sort_order降序
        MarketTopicExample example = new MarketTopicExample();
        example.setOrderByClause("sort_order desc");
        MarketTopicExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false).andIdNotEqualTo(id);
        List<MarketTopic> topicList = topicMapper.selectByExampleWithBLOBs(example);
        // 获取分页信息
        PageInfo<MarketTopic> pageInfo = new PageInfo<>(topicList);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return new TopicListVO(total, pages, limit, page, topicList);
    }
}
